package com.lepu.app.fun.my.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.BitmapLoadUtil;
import com.app.utils.Setting;
import com.core.lib.utils.main.UtilityBase;
import com.lepu.app.fun.my.bean.BeanLecture;
import com.lepu.pasm.R;

import java.util.ArrayList;

public class AdapterLecture extends BaseAdapter {

    private LayoutInflater mLayoutInflater = null;
    private ArrayList<BeanLecture> mArrayList = null;
    private Context mContext = null;

    public AdapterLecture(Context context, ArrayList<BeanLecture> arrayList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mArrayList = arrayList;
    }

    @Override
    public int getCount() {
        int size = 0;
        if (mArrayList != null && mArrayList.size() > 0) {
            size = mArrayList.size();
        }
        return size;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(
                    R.layout.my_lecture_fragment_listitem, null);

            holder = new ViewHolder();

            ImageView iconImageView = (ImageView) convertView
                    .findViewById(R.id.iconImageView);
            TextView titleTextView = (TextView) convertView
                    .findViewById(R.id.titleTextView);
            TextView teachPersonTextView = (TextView) convertView
                    .findViewById(R.id.teachPersonTextView);
            TextView teachTimeTextView = (TextView) convertView
                    .findViewById(R.id.teachTimeTextView);
            TextView statusTextView = (TextView) convertView
                    .findViewById(R.id.statusTextView);
            View splitView = (View) convertView
                    .findViewById(R.id.splitView);

            holder.iconImageView = iconImageView;
            holder.titleTextView = titleTextView;
            holder.teachPersonTextView = teachPersonTextView;
            holder.teachTimeTextView = teachTimeTextView;
            holder.statusTextView = statusTextView;
            holder.splitView = splitView;

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final BeanLecture item = mArrayList.get(position);
        BitmapLoadUtil.displayImage(R.drawable.default_icon_small, Setting.getImageUrl(item.ImageUrl), holder.iconImageView);

        holder.titleTextView.setText(item.ActivityName);
        holder.teachPersonTextView.setText(String.format("主讲人：%s", item.DoctorName));
        holder.teachTimeTextView.setText(String.format("讲座时间：%s", item.ActivityTimeText));//

        //int status = new Random().nextInt(3) + 1;
        int status = UtilityBase.parseInt(item.ActivityStatus);
        if (status == 2) {
            holder.statusTextView.setText("正在进行");
            holder.statusTextView.setBackgroundResource(R.drawable.index_category_bg_icon_green);
        }else if (status == 1) {
            holder.statusTextView.setText("即将开始");
            holder.statusTextView.setBackgroundResource(R.drawable.index_category_bg_icon_yellow);
        } else if (status == 3) {
            holder.statusTextView.setText("讲座回顾");
            holder.statusTextView.setBackgroundResource(R.drawable.index_category_bg_icon_gray);
        }else{
            holder.statusTextView.setText("即将开始");
            holder.statusTextView.setBackgroundResource(R.drawable.index_category_bg_icon_yellow);
        }

        if(position > 0){
            int statusPre = UtilityBase.parseInt(mArrayList.get(position-1).ActivityStatus);
            if(status == statusPre){
                holder.statusTextView.setVisibility(View.GONE);
                holder.splitView.setVisibility(View.GONE);
            }else{
                holder.statusTextView.setVisibility(View.VISIBLE);

                if(status == 2){
                    holder.splitView.setVisibility(View.GONE);
                }else{
                    holder.splitView.setVisibility(View.VISIBLE);
                }
            }
        }else{
            holder.statusTextView.setVisibility(View.VISIBLE);
            holder.splitView.setVisibility(View.GONE);
        }

        return convertView;
    }

    private static class ViewHolder {
        ImageView iconImageView;
        TextView titleTextView;
        TextView teachPersonTextView;
        TextView teachTimeTextView;

        TextView statusTextView;
        View splitView;
    }
}
